package com.my.chapter4;

public class practice5 {
    public static void main(String[] args) {
        int i = 0x1;
        int j = 0x2;
        System.out.println("i= "+toBinary(i)+"; j= "+toBinary(j));
        int k = i&j;
        System.out.println("i&j= "+toBinary(k));
        int o = i|j;
        System.out.println("i|j= "+toBinary(o));
        int m = i^j;
        System.out.println("i^j= "+toBinary(m));
        int n = ~i;
        int t = ~j;
        System.out.println("~i= "+toBinary(n)+"; ~j= "+toBinary(t));
    }
    static String toBinary(int n){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<32;i++){
            sb.append((n&1)==1?1:0);  //n与1进行按位与操作判断是否为1若为1则值为1，若不则为0
            n>>=1;//n每次右移一位
        }
        sb.reverse();
        return sb.toString();
    }
}
